package legacy

import (
	"testing"
)

func TestParameterValidation(t *testing.T) {
	testCases := []struct {
		name        string
		fontName    string
		fontSize    uint32
		fontWidth   uint32
		charSpacing uint32
		rotation    uint32
		x           uint32
		y           uint32
		text        string
		expectError bool
	}{
		{"正常参数", "Arial", 16, 16, 0, 0, 100, 200, "Hello", false},
		{"字号过小", "Arial", 11, 16, 0, 0, 100, 200, "Hello", true},
		{"字号过大", "Arial", 1001, 16, 0, 0, 100, 200, "Hello", true},
		{"字宽过小", "Arial", 16, 11, 0, 0, 100, 200, "Hello", true},
		{"字宽过大", "Arial", 16, 1001, 0, 0, 100, 200, "Hello", true},
		{"字间隔过大", "Arial", 16, 16, 1000000, 0, 100, 200, "Hello", true},
		{"旋转角度过大", "Arial", 16, 16, 0, 91, 100, 200, "Hello", true},
		{"X坐标过大", "Arial", 16, 16, 0, 0, 1000000, 200, "Hello", true},
		{"Y坐标过大", "Arial", 16, 16, 0, 0, 100, 1000000, "Hello", true},
		{"文本为空", "Arial", 16, 16, 0, 0, 100, 200, "", true},
	}
	
	for _, tc := range testCases {
		t.Run(tc.name, func(t *testing.T) {
			_, err := NewPacket(tc.fontName, tc.fontSize, tc.fontWidth, tc.charSpacing, 
				tc.rotation, false, false, uint32(StyleSolid), tc.x, tc.y, tc.text)
			
			if tc.expectError && err == nil {
				t.Error("期望出现错误但没有错误")
			}
			if !tc.expectError && err != nil {
				t.Errorf("不期望出现错误但出现了错误: %v", err)
			}
		})
	}
}

func TestUnmarshalErrors(t *testing.T) {
	testCases := []struct {
		name string
		data []byte
	}{
		{"数据包过短", []byte{0x5A, 0x4B}},
		{"错误的起始位", []byte{0xFF, 0xFF, 0x43, 0x54, 0x01, 0x00, 0x01}},
		{"错误的数据包类型", append([]byte{0x5A, 0x4B, 0x43, 0x54, 0x01, 0x00, 0x02}, make([]byte, MinPacketSize-7)...)},
		{"错误的停止位", func() []byte {
			// 创建一个有效的数据包但修改停止位
			packet, _ := NewPacket("Arial", 16, 16, 0, 0, false, false, uint32(StyleSolid), 0, 0, "test")
			data := packet.Marshal()
			// 修改停止位
			data[len(data)-2] = 0xFF
			data[len(data)-1] = 0xFF
			return data
		}()},
	}
	
	for _, tc := range testCases {
		t.Run(tc.name, func(t *testing.T) {
			_, err := Unmarshal(tc.data)
			if err == nil {
				t.Error("期望出现错误但没有错误")
			}
		})
	}
}

func TestChecksumValidation(t *testing.T) {
	// 创建一个有效的数据包
	packet, err := NewPacket("Arial", 16, 16, 0, 0, false, false, uint32(StyleSolid), 0, 0, "test")
	if err != nil {
		t.Fatalf("创建数据包失败: %v", err)
	}
	
	// 序列化
	data := packet.Marshal()
	
	// 修改校验和
	data[len(data)-4] = 0xFF // 校验和在倒数第4和第3个字节
	data[len(data)-3] = 0xFF
	
	// 尝试反序列化，应该失败
	_, err = Unmarshal(data)
	if err == nil {
		t.Error("期望校验和验证失败但没有失败")
	}
}

func TestUtilityFunctions(t *testing.T) {
	packet, err := NewPacket("Arial", 24, 20, 5, 30, true, true, uint32(StyleDotMatrix), 100, 200, "测试文本")
	if err != nil {
		t.Fatalf("创建数据包失败: %v", err)
	}
	
	// 测试字体名称获取
	fontName := packet.GetFontName()
	if fontName != "Arial" {
		t.Errorf("字体名称错误: 期望 'Arial', 得到 '%s'", fontName)
	}
	
	// 测试文本获取
	text := packet.GetText()
	if text != "测试文本" {
		t.Errorf("文本内容错误: 期望 '测试文本', 得到 '%s'", text)
	}
	
	// 测试加粗检查
	if !packet.IsBold() {
		t.Error("应该是加粗的")
	}
	
	// 测试斜体检查
	if !packet.IsItalic() {
		t.Error("应该是斜体的")
	}
	
	// 测试字符串表示
	str := packet.String()
	if str == "" {
		t.Error("字符串表示不应该为空")
	}
	t.Logf("数据包字符串表示: %s", str)
}

func BenchmarkMarshal(b *testing.B) {
	packet, _ := NewPacket("Arial", 16, 16, 0, 0, false, false, uint32(StyleSolid), 0, 0, "Benchmark test text")
	
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		_ = packet.Marshal()
	}
}

func BenchmarkUnmarshal(b *testing.B) {
	packet, _ := NewPacket("Arial", 16, 16, 0, 0, false, false, uint32(StyleSolid), 0, 0, "Benchmark test text")
	data := packet.Marshal()
	
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		_, _ = Unmarshal(data)
	}
}